﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_Project.Model;
using MVC_Project.Bll;
using MVC_Project.Bll.Systems;

namespace MVC_Project.UI.Controllers
{
    public class SignController : Controller
    {
        // GET: Sign
        public ActionResult Login()
        {
            string path = Request.QueryString["Redirect"];
            TempData["Redirect"] = path;
            return View();
        }

        [HttpPost]
        public ActionResult Login(string name,string pwd)
        {
            var reurl = TempData["Redirect"];

            dynamic d = new ExpandoObject();

            //对密码进行MD5加密
            string strpwd = MD5Helper.Md5(pwd);

            UsersBll ubll = new UsersBll();


            var userinfo = ubll.Query().Where(u => u.loginName == name && u.loginPwd == strpwd).FirstOrDefault();

            if (userinfo != null)
            {
                Session["uid"] = userinfo.userID;
                Session["rid"] = userinfo.roleID;

                Session["headimg"] = userinfo.Images.path;
                Session["name"] = userinfo.name;

                d.code = 1;
                d.msg = "登录成功";
                d.data = reurl;
            }
            else
            {
                d.code = -1;
                d.msg = "登录失败！";
            }

            return Content(JsonConvert.SerializeObject(d),"application/json");
        }

        public ActionResult Out() {
            Session.Clear();
            return Redirect("/Sign/Login");
        }
    }
}